package adminUser

import (
	session2 "OnlineRetailers/controllers/admin/session"
	"OnlineRetailers/controllers/errorDeal"
	"OnlineRetailers/models/Admin"
	error2 "OnlineRetailers/models/errorDeal"
	"encoding/json"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type UpdateAdminUser struct {
	beego.Controller
}

func (c *UpdateAdminUser) UpdateAdminUser() {

	defer func() {
		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.ServeJSON()
	}()

	var addr = "/admin/UpdateAdminUser"
	var err error

	user := &Admin.User{}
	log := &error2.Log{}
	mes := &error2.Mes{}

	o := orm.NewOrm()

	err = json.Unmarshal([]byte(c.GetString("user")), user)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	if user.Account == "" || user.Name == "" {
		c.Data["json"] = errorDeal.ErrLightMess(mes, "401", "信息不全")
		return
	}
	err = session2.CheckSession(c.Ctx.Request.Header["Session"], 3)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	_, err = o.Update(user)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	mes.Mes = "success"
	mes.Code = "200"
	c.Data["json"] = mes
}
